package bb.lanxing.network;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mapapi.SDKInitializer;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.util.Log;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;

public abstract class BiCiCallback implements Callback {
    public static final String TAG = "BiCiCallback";
    public static final String RESPONSE_RES = "res";
    private boolean autoCancelDialog;
    private BaseActivity baseActivity;
    private String value;

    public BiCiCallback() {
    }

    public BiCiCallback(BaseActivity baseActivity, boolean z) {
        this.baseActivity = baseActivity;
        this.autoCancelDialog = z;
    }

    public BiCiCallback(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        this.autoCancelDialog = true;
    }

    public abstract void onResponseString(@NonNull String str) throws JSONException;

    @Override
    public void onFailure(Call call, @NonNull IOException iOException) {
        onFailure(call.request(), iOException);
    }

    @Override
    public void onResponse(@NonNull Call call, @NonNull okhttp3.Response response) throws IOException {
        onResponse(response);
    }

    public void onFailure(Request request, IOException iOException) {
        onResponseFail(App.getContext().getString(R.string.network_err));
        BaseActivity baseActivity = this.baseActivity;
        if (baseActivity == null || !this.autoCancelDialog) {
            return;
        }
        baseActivity.dismissLoadingDialog();
    }

    public void onResponse(okhttp3.Response response) throws IOException {
        BaseActivity baseActivity = this.baseActivity;
        if (baseActivity != null && this.autoCancelDialog) {
            baseActivity.dismissLoadingDialog();
            this.baseActivity.closePopWindow();
        }
        try {
            String string = response.body().string();
            Log.i(TAG, "response : " + response + " body : " + string);
            if (response.code() == 200) {
                JSONObject jsonObject = new JSONObject(string);
                int code = jsonObject.getInt("code");
                if (code == 200) {
                    onResponseString(string);
                } else {
                    onResponseFail(jsonObject.getString("msg"));
                }
            } else if (response.code() == 403) {
                if (App.getContext().isUserSignin()) {
                    App.getContext().cleanSigninState();
                }
                App.getContext().userSignin();
            } else if (response.code() == 504) {
                onResponseFail(App.getContext().getString(R.string.network_err));
            } else if (response.code() == 204) {
                onResponseString(string);
            } else if (response.code() == 500) {
                onResponseFail(response.message());
            } else {
                onResponseFail(new JSONObject(string)
                        .getString(SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_MESSAGE));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            String message = response.message();
            if (TextUtils.isEmpty(message) || response.code() == 200) {
                return;
            }
            onResponseFail(message);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public void onResponseFail(String str) {
        App.getContext().showMessage(str);
    }
}
